package best.driver.android.Activity.Order.Adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.Event.HideMultiAddressEvent;
import best.driver.android.R;
import butterknife.ButterKnife;
import butterknife.InjectView;
import de.greenrobot.event.EventBus;

import java.util.ArrayList;
import java.util.List;

public class MultiAddressAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private List<OrderAddresses> mAddressItems = new ArrayList<OrderAddresses>();

    public MultiAddressAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderAddresses> list) {
        mAddressItems.clear();
        mAddressItems.addAll(list);
        if(list == null || list.size() == 0) {
            return;
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAddressItems.size();
    }

    @Override
    public OrderAddresses getItem(int position) {
        return mAddressItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.multi_address_item, parent, false);
            holder = new ViewHolder(convertView);
//            holder.mIvHide = (ImageView) convertView.findViewById(R.id.iv_hide);
//            holder.mTvLeft = (TextView) convertView.findViewById(R.id.tv_left);
            convertView.setTag(holder);
        }

        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        final OrderAddresses item = getItem(position);
        if(mAddressItems != null && mAddressItems.size() > 1) {
            if(position == 0) {
                holder.mIvHide.setVisibility(View.VISIBLE);
                holder.mIvHide.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        HideMultiAddressEvent event = new HideMultiAddressEvent();
                        event.flag = item.flag;
                        EventBus.getDefault().post(event);
                    }
                });
            } else {
                holder.mIvHide.setVisibility(View.GONE);
            }
            if(OrderStatus.RECEIVER_ADDRESS.equals(item.flag)) {
                holder.mTvLeft.setText("卸货地" + (position + 1));
            } else {
                holder.mTvLeft.setText("提货地" + (position + 1));
            }
            if(!TextUtils.isEmpty(item.address)) {
                holder.mTvRight.setText(item.address);
            }
            if(!TextUtils.isEmpty(item.addressDetail)) {
                holder.mTvRightDetail.setVisibility(View.VISIBLE);
                holder.mTvRightDetail.setText(item.addressDetail);
            } else {
                holder.mTvRightDetail.setVisibility(View.GONE);
            }
        } else if (holder != null && holder.mIvHide!=null){
            holder.mIvHide.setVisibility(View.GONE);
        }
    }

    static class ViewHolder {

        @InjectView(R.id.tv_left)
        TextView mTvLeft;

        @InjectView(R.id.tv_right)
        TextView mTvRight;

        @InjectView(R.id.tv_right_detail)
        TextView mTvRightDetail;

        @InjectView(R.id.iv_hide)
        ImageView mIvHide;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
